import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api/ajax.js'

Vue.use(Vuex);

let time;

let state = {
	count: 0,
	userInfo: {}, //用户信息
	loginState: false, //登录状态


	// 聊天相关
	// 用户列表
	friendList: [],
	// 当前聊天用户
	currUserID: "",
	// 未读消息数
	msgNum: 0,
	// 聊天记录
	msgList: [],
	// 时间戳
	msgTimeStamp: "",
	msgObj: {},
	// TabNum
	tabNum: 0,
	token: '',
	signInfo:{},
	
	myNum:0,
}

let getters = {
	doneTodos: state => {
		return state.count
	},
	getLoginState: state => {
		let token = uni.getStorageSync('xj_token');
		
		if (token) {
			state.loginState = true
			return true
		}
		return new Promise((resolve, reject) => {
			if (time) clearInterval(time)
			time = setInterval(() => {
				let token = uni.getStorageSync('xj_token')
				if (token) {
					clearInterval(time)
					state.loginState = true
					resolve(true)
				}
			}, 50)
		})
	},

	getToken: state => {
		if (!state.token) {
			state.token = uni.getStorageSync('xj_token');
		}
		return state.token
	},

	getMsgList: state => {
		return state.msgList
	},
	getBarBadge: state => {
		return state.BarBadge
	}
}

let mutations = {
	add(state) {
		state.count += 2
	},
	setToken(state, token) {
		state.token = token
	},
	// 设置用户信息
	setUser(state, user) {
		console.log('vuex look');
		state.userInfo = user;
	},
	setSign(state, sign){
		state.signInfo = sign;
	},
	// 设置登录状态
	login(state, islogin) {
		console.log(state.loginState, islogin)
		state.loginState = islogin;
	},
	currUserID(state, userId) {
		state.currUserID = userId;
	},
	addMsgList(state, msg) {

		msg.forEach(item => {
			if (item.conversationID != state.currUserID) {
				state.msgNum++;
			}
		});
		let msgArr = msg.sort((a, b) => a.orderKey - b.orderKey);
		state.msgList.push(...msgArr);
		state.msgTimeStamp = new Date().getTime();
	},
	setTabNum(state, num) {
		state.tabNum = num;
		if (num > 0) {
			uni.setTabBarBadge({
				index: 3,
				text: num.toString(),
				complete: () => {},
			});
		} else {
			uni.removeTabBarBadge({ index: 3 });
		}
	},
	setTabNum2(state, groupNum) {
		state.tabNum = groupNum;
		if (groupNum > 0) {
			uni.setTabBarBadge({
				index: 3,
				text: '.',
				complete: () => {},
			});
		} else {
			uni.removeTabBarBadge({ index: 3 });
		}
	},
	setTabMyNum(state, num) {
		state.myNum = num
		if (num > 0) {
			console.log('执行红点',num);
			uni.setTabBarBadge({
				index: 4,
				text: num.toString(),
				complete: () => {},
			});
		}else{
			console.log('清除红点',num);
			uni.removeTabBarBadge({ index: 4 });
		}
	}
}

let actions = {
	addCountAction(context) {
		context.commit('add')
	},
	async getMyNum({commit}){
		let res = await api.post('/Member/BrowseMySelfNum')
		let num = res.data || 0
		
		// let newre = await api.post('Appointment/MyRegister', {
		// 	PageIndex: 1,
		// 	PageSize: 999
		// });
		// let newre_arr = []
		let newre_num = uni.getStorageSync('BrowserNum');
		let count = num
		// if (newre.data) {
		// 	for (let i = 0; i < newre.data.length; i++) {
		// 		if (newre.data[i].Item[0].Status == 0 && newre.data[i].PayStatus!=0) {
		// 			newre_arr.push(newre.data[i])
		// 		}
		// 	}
		// 	newre_num = newre_arr.length
		// }
		count = Number(num) - Number(newre_num)
		commit('setTabMyNum',count)
	}
}

const store = new Vuex.Store({
	state: state,
	getters: getters,
	mutations: mutations,
	actions: actions
})

export default store